<?php 

class Events extends CWidget
{
    
    public $events = array();
    
    public function init()
    {

        
        $this->events = Program::model()->findAll('datum > NOW() - INTERVAL 1 DAY');
    }
 
    public function run()
    {
        
       Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl."/scripts/jquery.lightbox-0.5.pack.js");
       Yii::app()->clientScript->registerScriptFile("http://maps.google.com/maps/api/js?sensor=false");
       Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl."/css/jquery.lightbox-0.5.css" );
        $dateFormatter = new CDateFormatter('hu');
        
        foreach($this->events as $event){ ?>
        
<div class="event well">
    
    <div class="info">
        <h3><?php echo CHtml::tag('span', array('rel'=>$event->id,'class'=>'eventlink'), $event->nev); ?> <span class="raquo">&raquo;</span> </h3>
        
        <span class="date">
            <span class="month"><?php echo str_replace('á','Á',strtoupper($dateFormatter->format('MMM', strtotime($event->datum)))); ?></span>
            <span class="day"><?php echo date('j', strtotime($event->datum)); ?></span>
        </span>
        <span class="leiras" id="leiras<?php echo $event->id;?>"><?php echo substr($event->leiras, 0, 200); ?>...</span>
        <div id="longleiras<?php echo $event->id;?>" class="longleiras">
            <hr />
            <p>                
               <?php echo CHtml::link( CHtml::image($event->getFileUrl(),$event->nev, array('class'=>'eventimage')), $event->getFileUrl('large'), array('class'=>'eventThumbnail')); ?>                
                <?php echo $event->leiras; ?>
            </p>
            
            <div id="eventMap<?php echo $event->id;?>" class="eventmap" data-inited="0" data-address="<?php echo $event->address; ?>" data-icon="<?php echo $event->getIconUrl(); ?>"></div>
                        
        </div>
    </div>
</div>
            
<?php
        }
    }
}

Yii::app()->clientScript->registerScript('eventscript',
<<<SCRIPT

function showAddress(mapID, address, icon) {
    
   var map = new google.maps.Map(document.getElementById(mapID), {
          zoom: 14,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
   
   geocoder = new google.maps.Geocoder();
   geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        
        
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location,
            icon: icon,
        });
        
           var infowindow = new google.maps.InfoWindow({
                content: address
            });
            
           infowindow.open(map, marker);
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
       
}
//////////////////////////////////////////////////////////////////

$(function(){
  
  $('a.eventThumbnail').lightBox();
  
 $('.eventlink').click(function(){
    
    ref = $(this).attr('rel').replace('#','').replace('event','');

    $('#leiras'+ref).toggle();
    $('#longleiras'+ref).toggle();
    
    address = $('#eventMap'+ref).data('address');
    icon = $('#eventMap'+ref).data('icon');
    
    if($('#eventMap'+ref).data('inited') != '1'){
        
        $('#eventMap'+ref).data('inited','1');
        showAddress('eventMap'+ref, address, icon);
        
    }//endif
 })
    
});
SCRIPT
, CClientScript::POS_READY);

?>
